package com.echo.im.server.netty.processor.impl.handler;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.echo.im.common.enums.IMPlatformCmdType;
import com.echo.im.common.model.platform.UserLogoutCmd;
import com.echo.im.server.netty.context.UserChannelMap;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: echo
 * @time: 2024-08-16
 */
@Component
public class UserLogoutHandler implements PlatformCmdHandler<UserLogoutCmd> {
    @Override
    public void handle(UserLogoutCmd userLogoutCmdData) {
        Long userId = userLogoutCmdData.getUserId();
        List<Integer> terminalList = userLogoutCmdData.getTerminalList();

        for (Integer terminal : terminalList){
            Channel channel = UserChannelMap.getChannel(userId, terminal);

            if (channel != null){
                channel.close();
            }

        }
    }

    @Override
    public boolean support(Integer code) {
        return IMPlatformCmdType.USER_LOGOUT.code().equals(code);
    }

    @Override
    public UserLogoutCmd transForm(Object o) {
        String jsonString = JSONObject.toJSONString(o);
        return JSON.parseObject(jsonString, UserLogoutCmd.class);
    }
}
